
export class Plan{
    /**
     * @param {any[][]} map 
     * @returns {{top:Number,bottom:Number,right:Number,left:Number}}
     */
    static getRectangle(map){
        if(map.length === 0){
            return {top:0,bottom:0,right:0,left:0};
        }

        let maxHeight = map.length;
        let maxWidth = map[0].length;

        let top = maxHeight;
        let bottom = 0;

        let left = maxWidth;
        let right = 0;


        for(let i = 0; i<maxHeight; i++){//map[row][column]=>map[i][j]
            if(map[i] === undefined){
                continue;
            }
            for(let j = 0; j<maxWidth; j++){
                if(map[i][j] === undefined){
                    continue;
                }

                if(i>top){
                    top = i;
                }
                if(i<bottom){
                    bottom = i;
                }
                if(j>right){
                    right = j;
                }
                if(j<left){
                    left = j;
                }
            }
        }
        return {top:top,bottom:bottom,right:right,left:left};
    }
    /**
     * 
     * @param {any[][]} map 
     * @param {String|undefined} roomName 
     * @returns {{type:StructureConstant,pos:RoomPosition|{x:number,y:number}}[]}
     */
    static getList(map, roomName = undefined){

        if(map.length === 0){
            return [];
        }

        let maxHeight = map.length;
        let maxWidth = map[0].length;

        let list = [];

        let makePos = (x,y)=>new RoomPosition(x,y,roomName);
        if(roomName === undefined){
            makePos = (x,y)=>{
                return {x:x,y:y};
            };
        }
        for(let i = 0; i<maxHeight; i++){//map[row][column]=>map[i][j]
            if(map[i] === undefined){
                continue;
            }
            for(let j = 0; j<maxWidth; j++){
                if(map[i][j] === undefined){
                    continue;
                }
                list.push(makePos(x.y));
            }
        }
        return list;
    }
    constructor(map = []){
        // this.map = map;
        if(!map || map.length === 0){
            this.map = [];
            return;
        }
        let rect = Plan.getRectangle(map);
        this.map = [];
        for(let i=rect.top; i<=rect.bottom; i++){
            if(map[i] === undefined){
                this.map[i] = [];
                continue;
            }
            for(let j=rect.left; j<=rect.right; j++){
                this.map[i-rect.top][j-rect.left] = map[i][j];
            }
        }
    }
    getMaxSquareSide(){
        if(this.maxSquareSide !== undefined){
            return this.maxSquareSide;
        }
        if(this.map.length === 0){
            return this.maxSquareSide = 0;
        }
        let maxHeight = this.map.length;
        let maxWidth = this.map[0].length;

        let top = maxHeight;
        let bottom = 0;

        let left = maxWidth;
        let right = 0;


        for(let i = 0; i<maxHeight; i++){//map[row][column]=>map[i][j]
            if(this.map[i] === undefined){
                continue;
            }
            for(let j = 0; j<maxWidth; j++){
                if(this.map[i][j] === undefined){
                    continue;
                }

                if(i>top){
                    top = i;
                }
                if(i<bottom){
                    bottom = i;
                }
                if(j>right){
                    right = j;
                }
                if(j<left){
                    left = j;
                }
            }
        }

        return this.maxSquareSide = Math.max(bottom - top, right - left);
    }
    getMaxCircleRadius(){
        if(this.maxRadius !== undefined){
            return this.maxRadius;
        }
        if(this.map.length === 0){
            return this.maxRadius = 0;
        }

        let maxHeight = this.map.length;
        let maxWidth = this.map[0].length;

        let top = maxHeight;
        let bottom = 0;

        let left = maxWidth;
        let right = 0;


        for(let i = 0; i<maxHeight; i++){//map[row][column]=>map[i][j]
            if(this.map[i] === undefined){
                continue;
            }
            for(let j = 0; j<maxWidth; j++){
                if(this.map[i][j] === undefined){
                    continue;
                }

                if(i>top){
                    top = i;
                }
                if(i<bottom){
                    bottom = i;
                }
                if(j>right){
                    right = j;
                }
                if(j<left){
                    left = j;
                }
            }
        }
    }
}
export class RoomStructureHint{
    constructor(roomName){
        this.terrain = Game.map.getRoomTerrain(roomName);//todo

        let sideLength = 50;


        let outline = 2;
        this.distanceMap = [];
        for(let i=0; i<sideLength; i++){
            this.distanceMap[i] = [];
            for(let j=0; j<sideLength; j++){
                if((this.terrain.get(i,j) & (TERRAIN_MASK_WALL))){//|TERRAIN_MASK_LAVA))){
                    this.distanceMap[i][j] = 0;
                    continue;
                }
                if(i<outline || i>=sideLength - outline || j<outline || j>=sideLength - outline){
                    this.distanceMap[i][j] = 0;
                    continue;
                }
            }
        }

        let dist = 1;
        let touched = true;
        while(touched){
            touched = false;
            let j;
            let i;
            for(i=outline; i<sideLength - outline; i++){
                for(j=outline; j<sideLength - outline; j++){
                    if(this.distanceMap[i][j] !== undefined){
                        continue;
                    }
                        touched = true;
                    // for(let x=i-1; x<=i+1; x++){
                    //     for(let y=j-1; y<=j+1; y++){
                    //         if(this.distanceMap[x][y] === dist - 1){
                    //             this.distanceMap[i][j] = dist;
                    //             break;
                    //         }
                    //     }
                    //
                    //     if(this.distanceMap[i][j] === dist){
                    //         break;
                    //     }
                    // }

                    if(this.distanceMap[i-1][j-1] === dist - 1){
                        this.distanceMap[i][j] = dist;
                        continue;
                    }
                    if(this.distanceMap[i-1][j] === dist - 1){
                        this.distanceMap[i][j] = dist;
                        continue;
                    }
                    if(this.distanceMap[i-1][j+1] === dist - 1){
                        this.distanceMap[i][j] = dist;
                        continue;
                    }


                    if(this.distanceMap[i][j-1] === dist - 1){
                        this.distanceMap[i][j] = dist;
                        continue;
                    }

                    if(this.distanceMap[i][j+1] === dist - 1){
                        this.distanceMap[i][j] = dist;
                        continue;
                    }


                    if(this.distanceMap[i+1][j-1] === dist - 1){
                        this.distanceMap[i][j] = dist;
                        continue;
                    }
                    if(this.distanceMap[i+1][j] === dist - 1){
                        this.distanceMap[i][j] = dist;
                        continue;
                    }
                    if(this.distanceMap[i+1][j+1] === dist - 1){
                        this.distanceMap[i][j] = dist;
                        continue;
                    }

                }
            }
            dist++;
        }
    }
}